import router from '@/router/index'
import {computed} from "vue";
import store from "@/store";
import {SysMenu} from "@/api/sys/SysMenuApi";
import {Router} from "vue-router";

export default function (router: Router) {
    router.beforeEach((to, from, next) => {
        //跳转登陆页面,直接放行
        if (to.name === 'login') {
            next();
            return;
        }

        //没有用户信息或者菜单信息,则跳转登陆
        let asyncRoutes = computed<SysMenu[]>(() => store.state.localStorage.menuList);
        if (!asyncRoutes.value || asyncRoutes.value.length == 0) {
            next('/login');
            return;
        }
        next();
    })
}

export function existRoute(path: string) {

    let recordNormalizeds = router.getRoutes().filter(value => value.path === path).length;
    return recordNormalizeds > 0;
}